<?php

class //NAMECLASS// {

    function findAll($from, $to) {        
        $query_count = "SELECT count(*) FROM //nameclass//";
        $query = "//query_select// LIMIT $from, $to";
        $mysqli = new mysqli('localhost', 'root', '', 'cdcol');
        if ($mysqli->connect_errno) {
            printf("Connect failed: %s\n", $mysqli->connect_error);
            exit();
        }
        $posts = array();
        if ($result = $mysqli->query($query)) {
            while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
				//query_select_param_references_array//
			    //query_select_param_model_array//
                $posts[] = array('//nameclass//' => $//nameclass// //query_select_param_references_post//);
            }
            $result->close();
        }
        $rows = array();
        $rows[0] = 0;
        if ($result = $mysqli->query($query_count)) {
            $rows = $result->fetch_row();
            $result->close();
            $mysqli->close();
        }

        header('Content-type: application/json');
        echo json_encode(array('result' => $posts, 'rows' => $rows[0]));
    }

    function find($id) {
        $query = "SELECT //list_atrrs_select// FROM //nameclass// WHERE id = $id";

        $mysqli = new mysqli('localhost', 'root', '', 'cdcol');
        if ($mysqli->connect_errno) {
            printf("Connect failed: %s\n", $mysqli->connect_error);
            exit();
        }

        $posts = array();
        if ($result = $mysqli->query($query)) {
            while ($row = $result->fetch_object()) {
                $posts = array('//nameclass//' => $row);
            }
            $result->close();
            $mysqli->close();
        }

        header('Content-type: application/json');
        echo json_encode($posts);
    }

    function create(//list_atrrs_create_param//) {
        $query = "INSERT INTO //nameclass//(//list_atrrs_create//) values(//list_atrrs_create_values//)";

        $mysqli = new mysqli('localhost', 'root', '', 'cdcol');
        if ($mysqli->connect_errno) {
            printf("Connect failed: %s\n", $mysqli->connect_error);
            exit();
        }
        if ($mysqli->query($query)) {            
            header('Content-type: application/json');
            echo json_encode(array('message' => 'good'));            
        }else{
			header('Content-type: application/json');
			echo json_encode(array('message' => $mysqli->error));
		}
        $mysqli->close();
    }

    function update($id, //list_atrrs_create_param//) {
        $query = "UPDATE //nameclass// SET //list_atrrs_update// WHERE id = $id ";

        $mysqli = new mysqli('localhost', 'root', '', 'cdcol');
        if ($mysqli->connect_errno) {
            printf("Connect failed: %s\n", $mysqli->connect_error);
            exit();
        }
        if ($mysqli->query($query)) {
            $mysqli->close();
            header('Content-type: application/json');
            echo json_encode(array('message' => 'good'));
            return;
        }
        $mysqli->close();
        header('Content-type: application/json');
        echo json_encode(array('message' => 'error'));
    }

    function delete($id) {
        $query = "DELETE FROM //nameclass// WHERE id = $id ";

        $mysqli = new mysqli('localhost', 'root', '', 'cdcol');
        if ($mysqli->connect_errno) {
            printf("Connect failed: %s\n", $mysqli->connect_error);
            exit();
        }
        if ($mysqli->query($query)) {
            $mysqli->close();
            header('Content-type: application/json');
            echo json_encode(array('message' => 'good'));
            return;
        }
        $mysqli->close();
        header('Content-type: application/json');
        echo json_encode(array('message' => 'error'));
    }
    
    //list_atrrs_find//
    //find_references//
}

?>
